package com.wthink.hzgj.msgList

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import androidx.navigation.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.wthink.hzgj.R
import com.wthink.hzgj.business.data.Business
import com.wthink.hzgj.data.domain.BusDispatch
import com.wthink.hzgj.data.domain.NoticeMsg
import com.wthink.hzgj.databinding.FragmentMsgItemListBinding
import com.wthink.hzgj.msgList.adapter.MsgAdapter
import com.wthink.hzgj.msgList.viewmodel.MsgLisgItemViewmodel
import com.wthink.hzgj.util.ClickUtil
import com.wthink.hzgj.util.adapter.OnItemClickListener
import com.wthink.hzgj.util.closeDefaultAnimator


class MsgLisgItemFragment : Fragment(){
    private val viewModel by viewModels<MsgLisgItemViewmodel>()

    private lateinit var binding : FragmentMsgItemListBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_msg_item_list,
            container,
            false
        )


        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        binding.back.setOnClickListener{
            it.findNavController().popBackStack()
        }

        viewModel.start()

        binding.msgListView.layoutManager = LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)

        binding.msgListView.adapter = MsgAdapter().apply {

            mItemClickListener = object : OnItemClickListener {
                override fun onItemClick(obj: Any, position: Int) {
                    val notFastClick = ClickUtil.isNotFastClick()
                    if (notFastClick) {
                        val noticeMsg = obj as NoticeMsg
                        val args = Bundle()
                        //args.putInt("position",noticeMsg.index)
                        args.putInt("position",position)
                        Navigation.findNavController(binding.msgListView).navigate(R.id.msgListFragment,args)
                    }
                }
            }
        }
        closeDefaultAnimator(binding.msgListView)
    }
}